//
//  titleBtn.swift
//  微博
//
//  Created by xm on 2018/3/25.
//  Copyright © 2018年 xm. All rights reserved.
//

import UIKit

class TitleBtn: UIButton {

    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil{
            setTitle("首页", for: [])
        }else{
            setTitle(title! + "  ", for: [])
            setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        setTitleColor(UIColor.gray, for: [])
        titleLabel?.font=UIFont.boldSystemFont(ofSize: 17)
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局回调
    override func layoutSubviews() {
          super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
              let imageView = imageView else {
             return
        }
        if imageView.frame.origin.x==0 {// 避免累加，累加位置不对
            //左右移动
            titleLabel.frame=titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
            imageView.frame.origin.x=titleLabel.bounds.width
        }
    }

}
